var utils = require('../../../utils/utils.js');
Page({
  data: {
    info: []
  },
  tap: e => {
    console.warn(wx.appData.allUser)
    var employee = wx.appData.allUser[e.currentTarget.id];
    wx.req('attendanceTaskInfoGet', { user_id: employee.user_id }, '查询中').then((res) => {
      wx.req('userBasicInfoGet', { user_id: employee.id }).then((admin) => {
        wx.appData.user = admin.data.data[0];
        console.log(admin)
        var r = res.data.data, content = '该保安当前无任务，或任务已经超时。';
        for (var i = 0; i < r.length; i++) {
          let time = r[i].start_time.split(':');
          if (utils.compareTime(parseInt(time[0]), parseInt(time[1]))) {
            content = '该保安当前考勤任务在：\n' + r[i].name;
            break;
          }
        }
        wx.modal(employee.username, content, '考勤代签', '个人信息').then(() => {
          wx.navigateTo({ url: '../../my/info/info?employee=' + JSON.stringify(employee) })
        }).catch(() => {
          console.log(wx.appData.user)
          wx.modal('考勤代签', '您将为' + employee.username + 
            '将代签考勤点\n' + r[i].name,
            '取消', '确认代签'
          ).then(() => {
            var AHA_data = {
              user_id: employee.user_id,
              task_info_id: r[i].task_info_id,
            }
            console.warn(r[i], AHA_data)
            wx.req('attendanceHistoryAdd', AHA_data, '请稍候').then((res) => {
              console.log(res)
              if (res.data.errCode == 4062) wx.toast('考勤已完成', 'success')
              else if (res.data.errCode == 0) wx.toast('代签成功', 'success')
              else wx.toast('代签失败')
            })
          }).catch(() => {;})
        })
      })
    })
    
  },
  
  onShow:function(){
    var list = []
    for(var i = 0; i < wx.appData.allUser.length; i++){
      list.push({
        id: i,
        userInfo: wx.appData.allUser[i].idscrtmgr_userinfo,
        img: "",
        name: wx.appData.allUser[i].username,
        tele: wx.appData.allUser[i].telephoneNumber,
        info: wx.appData.allUser[i].baygw || '暂无信息',
      })
      this.setData({
        info : list
      })
    }
  },
})